package com.jy.horny.base

import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.Executors

/** 用于处理数据计算的子线程. 主要用于子线程处理paging2的数据变化 */
val workScheduler by lazy { Schedulers.from(Executors.newSingleThreadExecutor()!!) }

fun <T> Observable<T>.observeOnStore(): Observable<T> = observeOn(workScheduler)
fun <T> Observable<T>.subscribeOnIO(): Observable<T> = subscribeOn(Schedulers.io())
fun <T> Observable<T>.observeOnMain(): Observable<T> = observeOn(AndroidSchedulers.mainThread())

fun Disposable.bindLifecycle(owner: LifecycleOwner?) = this.apply {
    owner?.lifecycle?.addObserver(LifecycleObserver(this))
}

//fun Disposable.bindLifecycle(view: View) =
//        this.apply {
//            (view.activity as? LifecycleOwner?)?.lifecycle?.addObserver(LifecycleObserver(this))
        //}